package com.sky.controller.common;


import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
@Slf4j
@RequestMapping("/admin/common")
public class CommonController {

    @Autowired
    private AliOssUtil aliOssUtil;


    //文件上传功能

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws IOException {
        log.info("文件上传");

        try {
            //原始文件名
            String fileName = file.getOriginalFilename();
            //截取原始文件名的后缀
            String substring = fileName.substring(fileName.lastIndexOf("."));
            String objName = UUID.randomUUID().toString()+substring;

            String file_path = aliOssUtil.upload(file.getBytes(), objName);
            return Result.success(file_path);
        } catch (IOException e) {
            log.error("文件上传失败:{}", e.toString());
        }

        return Result.error("文件上传失败");

        //需要返回文件上传路径(阿里云绝对路径)
    }
}
